﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Common.Silverlight;
using System.Windows.Data;
using SMS.Model;
using SMS.Entities;
using SMS.Views.Reports;

namespace SMS.ViewModel.Reports
{
    public class CustomerReportViewModel : BaseReport
    {
        ReportModel _model;
        public CustomerReportViewModel()
        {
            SearchCommand = new DelegateCommand(Search);
            _model = new ReportModel();
            _model.OnCustomerReportDataCompleted += new Action<System.Collections.ObjectModel.ObservableCollection<Entities.Report.CustomerReport>>(_model_OnCustomerReportDataCompleted);
            _model.OnServiceError += new Action<SMSException>(_model_OnServiceError);
        }

        public event Action<string> OnReportChanged;
        void _model_OnCustomerReportDataCompleted(System.Collections.ObjectModel.ObservableCollection<Entities.Report.CustomerReport> obj)
        {
            if (obj != null)
            {
                PagedCollectionView paged = new PagedCollectionView(obj);
                ReportCollection = paged;
                if (OnReportChanged != null)
                    OnReportChanged(this.SelectedSearchOption);
            }
        }

        void _model_OnServiceError(SMSException obj)
        {
            ErrorMessage = obj.Message;
            UserMessage = string.Empty;
        }

        public ICommand SearchCommand { get; set; }
        private void Search(object param)
        {
            _model.GetCustomerDetailReportData(App.LoggedinUserName, FromDate, EndDate, SelectedSearchOption == null ? "" : SelectedSearchOption);
        }

        private PagedCollectionView _ReportCollection;

        public PagedCollectionView ReportCollection
        {
            get { return _ReportCollection; }
            set { _ReportCollection = value; OnPropertyChanged("ReportCollection"); }
        }

        private CustomerReport _SelectedReportData;

        public CustomerReport SelectedReportData
        {
            get { return _SelectedReportData; }
            set { _SelectedReportData = value; OnPropertyChanged("SelectedReportData"); }
        }
    }
}
